﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using Senparc.Weixin.Helpers.Extensions;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.AdvancedAPIs.MailList;
using Senparc.Weixin.Work.AdvancedAPIs.OaDataOpen;
using Senparc.Weixin.Work.Containers;
using Senparc.Weixin.Work.Helpers;
using WeiXinAPI.Common;

namespace WeiXinAPI
{
    [Description("获取企业微信中所有员工的打卡记录")]
    public class WxCheckinHandler: BaseHandler
    {
        [MustLogin]
        [Description("获取打卡记录信息")]
        [ReturnType(typeof(IList<GetCheckinDataJsonResult_Result>))]
        public ApiResult GetCheckinData(string wxConfig)
        {
            var wxInfo = WXBaseHandler.TryGetWxCorpInfo(wxConfig);
            if (wxInfo.CorpInfo == null)
            {
                return Error(wxInfo.Message);
            }
            //获取用户列表代理ID和密钥获取令牌
            var accessToken = AccessTokenContainer.TryGetToken(wxInfo.CorpInfo.CorpID, wxInfo.CorpInfo.CommunicationSercet);

            //获取用户列表代理ID和密钥获取令牌
            var accessTokenCheckin = AccessTokenContainer.TryGetToken(wxInfo.CorpInfo.CorpID, "bdKhRKLF0ZKHIbMRn8E_pFaQOI0CnN6FUxFj74X0MPA");

            DateTime startTime = Convert.ToDateTime("2018-07-19 00:00:00");
            DateTime endTime = Convert.ToDateTime("2018-07-19 23:59:59");

            //获取全部的成员
            var userList = MailListApi.GetDepartmentMember(accessToken, 1, 1).userlist;
            int userNum = userList.Count;
            if (userNum <= 100)
            {
                string[] useridItem = new string[userNum];
                for (int i = 0; i < userNum; i++)
                {
                    useridItem[i] = userList[i].userid;
                }
                var CheckinList = OaDataOpenApi.GetCheckinData(accessTokenCheckin, OaDataOpenApi.OpenCheckinDataType.上下班打卡, startTime, endTime, useridItem).checkindata;

                return Success(CheckinList);
            }
            else
            {
                int forNum = userNum / 100;
                int j = 0;
                for (int i = 0; i < forNum; i++)//控制循环每次取100个用户
                {
                    j = i * 100;
                    string[] useridItem = new string[100];
                    int index = 0;
                    for (; j < userNum; j++)
                    {
                        useridItem[index] = userList[j].userid;
                        index = index + 1;
                        if ((j + 1) % 100 == 0)
                        {
                            break;
                        }
                    }
                    var CheckinList = OaDataOpenApi.GetCheckinData(accessTokenCheckin, OaDataOpenApi.OpenCheckinDataType.上下班打卡, startTime, endTime, useridItem).checkindata;
                }

                if ((forNum * 100) < userNum) //取出剩余的用户数
                {
                    j = forNum * 100;
                    string[] useridItem = new string[userNum-(forNum * 100)];
                    int index = 0;
                    for (; j < userNum; j++)
                    {
                        useridItem[index] = userList[j].userid;
                        index = index + 1;
                    }
                    var CheckinList = OaDataOpenApi.GetCheckinData(accessTokenCheckin, OaDataOpenApi.OpenCheckinDataType.上下班打卡, startTime, endTime, useridItem).checkindata;
                }

                return Success(0);
            }
        }
    }
}
